package com.pay.mapper;

import com.pay.pojo.Token;
import com.pay.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;


public interface UserMapper {

    /**
     * 新增账号
     *
     * @param user
     * @return
     */
    @Insert("INSERT INTO user_mail ( email, password, salt, confirm_code, activation_time, is_valid ) " +
            " VALUES ( #{email}, #{password}, #{salt}, #{confirmCode}, #{activationTime}, #{isValid} )")
    int insertUser(User user);

    /**
     * 根据确认码查询用户
     *
     * @param confirmCode
     * @return
     */
    @Select("SELECT email, activation_time FROM user_mail WHERE confirm_code = #{confirmCode} AND is_valid = 0")
    User selectUserByConfirmCode(@Param("confirmCode") String confirmCode);

    /**
     * 根据确认码查询用户并修改状态值为 1（可用）
     *
     * @param confirmCode
     * @return
     */
    @Update("UPDATE user_mail SET is_valid = 1 WHERE confirm_code = #{confirmCode}")
    int updateUserByConfirmCode(@Param("confirmCode") String confirmCode);

    /**
     * 根据邮箱查询用户
     *
     * @param email
     * @return
     */
    @Select("SELECT * FROM user_mail WHERE email = #{email}")
    List<User> selectUserByEmail(@Param("email") String email);

    /**
     * 验证码新增
     *
     * @param checkCode
     * @return
     */
    @Insert("INSERT INTO user_mail ( email, checkcode ) " +
            " VALUES ( #{email}, #{checkCode} )")
    int insertCheckUser(@Param("email") String email,@Param("checkCode")String checkCode);

    @Delete("delete from user_mail where checkcode = #{checkCode}")
    void deleteUserRegistCode(@Param("checkCode") String checkCode);

    @Insert("insert into user_token(email , token)"+
            "values (#{tokenInfo.email} , #{tokenInfo.token})")
    void createToken(@Param("tokenInfo") Token tokenInfo);

    @Select("select email from user_mail where token = #{token}")
    String getToken(String token);
}
